<?php
/**
 * Instalacja obslugiwanych domen
 * Możliwość wskazania kilka domen na jedną
 * 
 */
	if(isset($_SERVER['HTTP_HOST'])) {
		$d__ = preg_replace ('#^www.#', '', $_SERVER['HTTP_HOST']);
	}else{
		$d__ = 'web.lex';
	}
		
	if (!defined('HOST')) {

//		// uruchomienie bazy konfiguracyjnej z listą domen
//		try {
//			$db = new PDO('sqlite:../database/setup.db',null,null,array(PDO::ATTR_PERSISTENT => ''));
//			$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//			$connected = is_object($db);
//		}
//		catch(PDOException $e) {
//			die('Error connecting to database.: '.$e->getMessage());
//		}
		
		
	
		$map = array(
					'lex.etriton.pl'	=>	array('name'=>'web.lex','status'=>'active','lang'=>'pol','redirect'=>false,'main_domain'=>'nieruchomosci-lex.pl'),
				    'nieruchomosci-lex.pl'	=>	array('name'=>'web.lex','status'=>'active','lang'=>'pol','redirect'=>false,'main_domain'=>null),
		// localhost
					'web.lex'	=> array('name'=>'web.lex','status'=>'active','lang'=>'pol','redirect'=>false,'main_domain'=>'vindcharter.se')
		);
		
		if (array_key_exists($d__,$map)) {
			if($map[$d__]['redirect']){
				header('HTTP/1.1 301 Moved Permanently');
				header('Location: http://www.'.$map[$d__]['main_domain'].$_SERVER['REQUEST_URI']);
				header('Connection: close');
			}
				
			define('HOST',$map[$d__]['name']);
			Configure::write('Config.host', $map[$d__]['name']);
			Configure::write('Config.status', $map[$d__]['status']);
		}else{
			define('HOST', 'web.lex');
			Configure::write('Config.host', 'web.lex');
			Configure::write('Config.status', 'active');
			$d__ = 'web.lex';
		}
		
	}
	
	/**
	 * definicja sciezki do zapisywania plikow 
	 * dla modelu upload
	 */
	if (!defined('UPLOAD_PATH')) {
	    define('UPLOAD_PATH', 'files/'.HOST.'/upload');
	}	
	
	
/**
 * Instalacja wersji językowej
 * @var unknown_type
 */	
    $languages = array(
    	 'pol'=>array('name'=>__("Polih",true))
    	,'eng'=>array('name'=>__("Angielski",true))
    );
	
	
	// zainstalowane jezyki
	Configure::write('Config.languages', $languages);	

	Configure::write('Config.default_lang', $map[$d__]['lang']);
	
/**
 * Definicja grup uzytkowników
 */
	$users_groups = array(
		'admin'=>array('name'=>__("Administrator",true))
		,'manag'=>array('name'=>__("Menadzer",true))		
		,'emplo'=>array('name'=>__("Pracownik",true))		
		,'copyr'=>array('name'=>__("Coypraiter",true))
		,'users'=>array('name'=>__("Uzytkownicy",true))
		,'firms'=>array('name'=>__("Firmy",true))
	);
	Configure::write('Config.users_groups', $users_groups);
	
/**
 * Google
 */

	Configure::write('Config.google.path',"files/".HOST."/google/");
	if(isset($_SERVER['HTTP_HOST']))Configure::write('Config.google.apikey',"apikey.".preg_replace ('#^www.#', '', $_SERVER['HTTP_HOST']).".txt");
	if(isset($_SERVER['HTTP_HOST']))Configure::write('Config.google.analytics',"analytics.".preg_replace ('#^www.#', '', $_SERVER['HTTP_HOST']).".txt");

	
/**
 * Newsletter
 */
$subject = Configure::read('Newsletter.unsubscribe_subject');
$subject = Configure::read('Newsletter.subscribe_subject');
$from = Configure::read('Newsletter.from'); #Required
$from_email = Configure::read('Newsletter.from_email'); #Required

$subject = Configure::read('Newsletter.sendX'); #Number of emails to sent at each admin_send call.
$subject = Configure::read('Newsletter.sendInterval'); #the interval time before send next batch
$subject = Configure::read('Newsletter.mail_opt_out_message');
$subject = Configure::read('Newsletter.emptyImagePath');

	
	/**
	 * uniqiname for uploaded file
	 * @param $fileName
	 */
	function uniqid_file_name($fileName){
		preg_match("/\.([^\.]+)$/", $fileName, $matches);
		return uniqid('file_').'.'.strtolower($matches[1]);
	}